//
//  MainTabBarController.swift
//  晓庄微博
//
//  Created by Maxwell on 10/15/18.
//  Copyright © 2018 Maxwell. All rights reserved.
//

import UIKit

class MainTabBarController: UITabBarController {

   private var btn = UIButton(imageName: "tabbar_compose_icon_add", backImageName: "tabbar_compose_button")
    
    var userLogin:Bool!
    var access_token:String!
    
    init(userLogin:Bool){
        self.userLogin = userLogin
        self.access_token = " "  //没有用户数据
        super.init(nibName:nil, bundle:nil)
    }
    
    init(userLogin:Bool,access_token:String){
        self.userLogin = userLogin
        self.access_token = access_token
        super.init(nibName:nil, bundle:nil)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        tabBar.tintColor = UIColor.orange
        let home:HomeTableViewController!
        let discover:DiscoverTableViewController!
        let message:MessageTableViewController!
        let profiles:ProfilesTableViewController!
        
        if userLogin {
            home = HomeTableViewController(userLogin:userLogin,access_token:access_token)
            discover = DiscoverTableViewController(userLogin:userLogin,access_token:access_token)
            message = MessageTableViewController(userLogin:userLogin,access_token:access_token)
            profiles = ProfilesTableViewController(userLogin:userLogin,access_token:access_token)
        }else{
            home = HomeTableViewController(userLogin:userLogin)
            discover = DiscoverTableViewController(userLogin:userLogin)
            message = MessageTableViewController(userLogin:userLogin)
            profiles = ProfilesTableViewController(userLogin:userLogin)
        }
        
        addChildTab(tab: home, imageName: "tabbar_home", titileName: "微博")
        addChildTab(tab: discover, imageName: "tabbar_discover", titileName: "发现")
        
        addChild(UIViewController()) //占个位置
        addBtn()
        
        addChildTab(tab: message, imageName: "tabbar_message_center", titileName: "消息")
        addChildTab(tab: profiles, imageName: "tabbar_profile", titileName: "我")
        
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        tabBar.bringSubviewToFront(btn)
    }
    func addBtn(){
        tabBar.addSubview(btn)
        //调整button的位置
        let w = tabBar.bounds.width / 5
        let h = tabBar.bounds.height
        
        btn.frame = CGRect(x: 2*w, y: 0, width: w , height: h)
        //添加点击事件
        btn.addTarget(self, action: #selector(clickBtn), for: UIControl.Event.touchUpInside)
    }
    //按钮点击相应
    @objc func clickBtn(){
        print("被点击了")
    }
    func addChildTab(tab:UITableViewController,imageName:String,titileName:String){
        //1.创建一个tab
        
        tab.tabBarItem.image = UIImage(named: imageName)
        tab.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")
        tab.tabBarItem.title = titileName
        
        tab.navigationItem.title = titileName
        //2.创建导航
        
        let nav = UINavigationController()
        
        nav.addChild(tab)
        
        //3.把导航加到当前视图
        addChild(nav)
        //修改这个文件
    }

}
